//
//  ChatManagerModule.m
//  RNEaseMob
//
//  Created by Xiaosong Gao on 2018/6/13.
//  Copyright © 2018年. All rights reserved.
//

#import "ChatManagerDelegate.h"
#import "Client.h"
#import "NSObject+Util.h"
#import <AudioToolbox/AudioToolbox.h>
#import <UserNotifications/UserNotifications.h>

static NSString *eventType = @"ChatManagerDelegate";

@implementation ChatManagerDelegate

DEFINE_SINGLETON_FOR_CLASS(ChatManagerDelegate);

RCT_EXPORT_MODULE();

#pragma mark - EMChatManagerDelegate

- (void)messagesDidReceive:(NSArray *)aMessages {
    UIApplicationState state = [[UIApplication sharedApplication] applicationState];
    // App在后台
    if (state == UIApplicationStateBackground) {
        //发送本地推送
        if (NSClassFromString(@"UNUserNotificationCenter")) { // ios 10
            // 设置触发时间
            UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.01 repeats:NO];
            UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
            content.sound = [UNNotificationSound defaultSound];
            // 提醒，可以根据需要进行弹出，比如显示消息详情，或者是显示“您有一条新消息”
            content.body = [NSString stringWithFormat:@"您有%i条新消息", aMessages.count];
            EMMessage *msg = [aMessages lastObject];
            UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:msg.messageId content:content trigger:trigger];
            [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:nil];
        }else {
            UILocalNotification *notification = [[UILocalNotification alloc] init];
            notification.fireDate = [NSDate date]; //触发通知的时间
            notification.alertBody = [NSString stringWithFormat:@"您有%i条新消息", aMessages.count];
            notification.alertAction = @"Open";
            notification.timeZone = [NSTimeZone defaultTimeZone];
            notification.soundName = UILocalNotificationDefaultSoundName;
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        }
    } else {
        AudioServicesPlaySystemSound(1007);
    }
    NSMutableArray *dicArray = [NSMutableArray array];
    [aMessages enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [dicArray addObject:[obj objectToDictionary]];
    }];
    [Client sendEventByType:eventType subType:@"messageDidReceive" data:dicArray];
}

- (void)cmdMessagesDidReceive:(NSArray *)aCmdMessages {
    NSMutableArray *dicArray = [NSMutableArray array];
    [aCmdMessages enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [dicArray addObject:[obj objectToDictionary]];
    }];
    [Client sendEventByType:eventType subType:@"cmdMessageDidReceive" data:dicArray];
}

- (void)conversationListDidUpdate:(NSArray *)aConversationList {
    NSMutableArray *dicArray = [NSMutableArray array];
    [aConversationList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [dicArray addObject:[obj objectToDictionary]];
    }];
    [Client sendEventByType:eventType subType:@"conversationListDidUpdate" data:dicArray];
}

@end
